home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / cpphtp2 / cpphtp2.jar / chpt_10.gml < prev    next >
Text File  |  1998-03-03  |  86KB  |  2,023 lines

  1. <html>
  2. <chapter>
  3. <section type=Popup name=Terminology title="Terminology">
  4. <page>
  5. <font size=14>
  6. A<br>
  7. abstract base class 
  8. <a href="%s4p0"><img src=iicons/bullbib.gif></a>
  9.  
  10. <a href="%s5p8"><img src=iicons/bullbib.gif></a>
  11. <a href="%s6p3"><img src=iicons/bullbib.gif></a>
  12. <a href="%s9p0"><img src=iicons/bullbib.gif></a>
  13. <br>
  14. abstract class 
  15. <a href="%s4p0"><img src=iicons/bullbib.gif></a>
  16. <a href="^Engineer::c:s0p6"><img src=iicons/bullbib.gif></a>
  17. <a href="%s9p3"><img src=iicons/bullbib.gif></a>
  18.  
  19. <a href="%s10p3"><img src=iicons/bullbib.gif></a>
  20. <br>
  21. B<br>
  22. base class 
  23. <a href="%s5p1"><img src=iicons/bullbib.gif></a>
  24. <a href="%s6p10"><img src=iicons/bullbib.gif></a>
  25. <br>
  26. C<br>
  27. class hierarchy 
  28. <a href="^Engineer::c:s0p6"><img src=iicons/bullbib.gif></a>
  29. <a href="%s5p8"><img src=iicons/bullbib.gif></a>
  30.  
  31. <a href=""><img src=iicons/bullbib.gif></a>
  32. <a href=""><img src=iicons/bullbib.gif></a>
  33. <br>
  34. concrete class 
  35. <a href="%s4p1"><img src=iicons/bullbib.gif></a>
  36. <a href="%s5p5"><img src=iicons/bullbib.gif></a>
  37. <br>
  38. D<br>
  39. derived class 
  40. <a href="%s5p1"><img src=iicons/bullbib.gif></a>
  41. <br>
  42. </font>
  43.  
  44. </page>
  45. <page>
  46. <font size=14>
  47. derived-class destructor 
  48.  
  49. <a href="%s8p1"><img src=iicons/bullbib.gif></a>
  50. <br>
  51. dynamic binding 
  52. <a href="%s3p2"><img src=iicons/bullbib.gif></a>
  53. <a href="%s6p9"><img src=iicons/bullbib.gif></a>
  54.  
  55. <a href="%s6p9"><img src=iicons/bullbib.gif></a>
  56. <a href="%s7p0"><img src=iicons/bullbib.gif></a>
  57. <a href="%s9p9"><img src=iicons/bullbib.gif></a>
  58. <a href="%s10p0"><img src=iicons/bullbib.gif></a>
  59. <a href="^Perform::c:s0p0"><img src=iicons/bullbib.gif></a>
  60. <br>
  61. E<br>
  62. extensibility 
  63. <a href="^Engineer::c:s0p8"><img src=iicons/bullbib.gif></a>
  64. <br>
  65. I<br>
  66. implementation 
  67. inheritance 
  68. <a href="^Engineer::c:s0p12"><img src=iicons/bullbib.gif></a>
  69. <br>
  70. independent software 
  71. vendor (ISVs) 
  72. <a href="%s7p1"><img src=iicons/bullbib.gif></a>
  73. <br>
  74. inheritance 
  75. <a href="%s7p1"><img src=iicons/bullbib.gif></a>
  76. <br>
  77. interface inheritance 
  78. <a href="^Engineer::c:s0p13"><img src=iicons/bullbib.gif></a>
  79. <br>
  80. L<br>
  81. </font>
  82.  
  83. </page>
  84. <page>
  85. <font size=14>
  86. late binding 
  87. <a href="%s7p0"><img src=iicons/bullbib.gif></a>
  88. <br>
  89. O<br>
  90. offset 
  91. <a href="%s10p7"><img src=iicons/bullbib.gif></a>
  92. <br>
  93. overridden <b>virtual</b> 
  94. function 
  95. <a href="%s7p2"><img src=iicons/bullbib.gif></a>
  96. <br>
  97. override a pure <b>virtual</b> 
  98. function 
  99. <a href="%s9p2"><img src=iicons/bullbib.gif></a>
  100. <br>
  101. P<br>
  102. pointer to a derived-class 
  103. object 
  104. <a href="%s5p3"><img src=iicons/bullbib.gif></a>
  105. <br>
  106. polymorphism 
  107. <a href="%s1p0"><img src=iicons/bullbib.gif></a>
  108. <a href="^Engineer::c:s0p0"><img src=iicons/bullbib.gif></a>
  109.  
  110. <a href="%s5p0"><img src=iicons/bullbib.gif></a>
  111. <a href="%s5p1"><img src=iicons/bullbib.gif></a>
  112. <a href="%s6p0"><img src=iicons/bullbib.gif></a>
  113. <a href="%s6p12"><img src=iicons/bullbib.gif></a>
  114. <a href="%s7p0"><img src=iicons/bullbib.gif></a>
  115. <a href="%s10p0"><img src=iicons/bullbib.gif></a>
  116. <br>
  117. pure <b>virtual</b> function 
  118. <a href="%s4p2"><img src=iicons/bullbib.gif></a>
  119.  
  120. <a href="^Engineer::c:s0p6"><img src=iicons/bullbib.gif></a>
  121. <a href="%s6p2"><img src=iicons/bullbib.gif></a>
  122. <a href="%s9p0"><img src=iicons/bullbib.gif></a>
  123. <a href="%s10p3"><img src=iicons/bullbib.gif></a>
  124. <br>
  125. </font>
  126.  
  127. </page>
  128. <page>
  129. <font size=14>
  130. S<br>
  131. software reuse 
  132. <a href="%s7p1"><img src=iicons/bullbib.gif></a>
  133. <br>
  134. static binding 
  135. <a href="%s3p2"><img src=iicons/bullbib.gif></a>
  136. <a href="%s6p8"><img src=iicons/bullbib.gif></a>
  137. <a href="%s6p9"><img src=iicons/bullbib.gif></a>
  138.  
  139. <a href="%s9p6"><img src=iicons/bullbib.gif></a>
  140. <br>
  141. <b>switch</b> logic 
  142. <a href="%s2p0"><img src=iicons/bullbib.gif></a>
  143. <br>
  144. V<br>
  145. <b>virtual</b> destructor 
  146. <a href="^Practice::c:s0p2"><img src=iicons/bullbib.gif></a>
  147. <br>
  148. <b>virtual</b> function 
  149. <a href="^Engineer::c:s0p0"><img src=iicons/bullbib.gif></a>
  150. <a href="%s5p1"><img src=iicons/bullbib.gif></a>
  151.  
  152. <a href="%s6p0"><img src=iicons/bullbib.gif></a>
  153. <a href="%s6p11"><img src=iicons/bullbib.gif></a>
  154. <a href="%s7p0"><img src=iicons/bullbib.gif></a>
  155. <a href="%s9p0"><img src=iicons/bullbib.gif></a>
  156. <a href="%s9p2"><img src=iicons/bullbib.gif></a>
  157. <a href="%s9p8"><img src=iicons/bullbib.gif></a>
  158.  
  159. <a href="%s10p0"><img src=iicons/bullbib.gif></a>
  160. <a href="%s10p6"><img src=iicons/bullbib.gif></a>
  161. <br>
  162. <b>virtual</b> function table 
  163. (vtable) 
  164. <a href="%s10p2"><img src=iicons/bullbib.gif></a>
  165. <br>
  166. vtable 
  167. <a href="%s10p2"><img src=iicons/bullbib.gif></a>
  168. <a href="%s10p4"><img src=iicons/bullbib.gif></a>
  169. <a href="%s10p10"><img src=iicons/bullbib.gif></a>
  170. <br>
  171. vtable pointer 
  172. <a href="%s10p10"><img src=iicons/bullbib.gif></a>
  173. <br>
  174. </font>
  175.  
  176. </page>
  177. </section>
  178. <section type=Popup name=Quotes title="Quotes">
  179. <page>
  180. <i>One Ring to rule them all, 
  181. One Ring to find them, 
  182. One Ring to bring them 
  183. all and in the darkness 
  184. bind them.</i>  <br>
  185. John Ronald Reuel 
  186. Tolkien, The Fellowship 
  187. of the Ring<br>
  188. <br>
  189.  
  190. </page>
  191. <page>
  192. <i>The silence often of pure 
  193. innocence               
  194. Persuades when speaking 
  195. fails.</i>   <br>
  196. William Shakespeare, 
  197. The Winter's Tale<br>
  198. <br>
  199.  
  200. </page>
  201. <page>
  202. <i>General propositions do 
  203. not decide concrete cases.</i> <br>
  204. Oliver Wendell Holmes<br>
  205. <br>
  206.  
  207. </page>
  208. <page>
  209. <i>A philosopher of 
  210. imposing stature doesn't 
  211. think in a vacuum. Even 
  212. his most abstract ideas 
  213. are, to some extent, 
  214. conditioned by what is or 
  215. is not known in the time 
  216. when he lives.</i>  <br>
  217. Alfred North Whitehead<br>
  218. <br>
  219.  
  220. </page>
  221. </section>
  222. <section type=Popup name=Illustration title="Illustrations">
  223. <page>
  224. <a href="^Code::c:s0p0">Fig. 10.1</a>  Demonstrating polymorphism with the <b>Employee</b> class hierarchy.<br>
  225. <a href="^Code::c:s0p1">Fig. 10.2</a>  Definition of abstract base class <b>Shape</b>.<br>
  226. <a href="^Illustration::c:s0p1">Fig. 10.3</a>  Flow of control of a virtual function call.<br>
  227. <br>
  228.  
  229. </page>
  230. <page>
  231. <font size=18><a href="~audio/Ch10/10fig003.au"><img src="bckgrnds/icons/audio.gif" align=sidebar></a>Figure 10.3 - Flow of control of a <b>virtual</b> function call. (Part 1 of 3)</font><br>
  232. <img src="graphics/ch10/fig1003a.gif" ><br>
  233.  
  234. </page>
  235. <page>
  236. <font size=18>Figure 10.3 - Flow of control of a <b>virtual</b> function call. (Part 2 of 3) </font><br>
  237. <img src="graphics/ch10/fig1003b.gif" ><br>
  238.  
  239. </page>
  240. <page>
  241. <font size=18>Figure 10.3 - Flow of control of a <b>virtual</b> function call. (Part 3 of 3) </font><br>
  242. <img src="graphics/ch10/fig1003c.gif" ><br>
  243.  
  244. </page>
  245. </section>
  246. <section type=Popup name=Answers title="Answers">
  247. <page pagename="Answer 10.1">
  248. <b>Answer 10.1</b><br>
  249. a) <b>switch</b>.<br>
  250. b) <b>= 0</b>.<br>
  251. c) abstract base class.<br>
  252. d) static.<br>
  253. e) dynamic.<br>
  254. <foreign  name="exercises" url="^Exercises::c:s0p0">
  255. <br>
  256.  
  257. </page>
  258. <page pagename="Answer 10.6">
  259. <b>Answer 10.6</b><br>
  260. Static binding is performed at compile-time when a function is called via a 
  261. specific object or via a pointer to an object. Dynamic binding is performed at run-
  262. time when a <tt><b>virtual</b></tt> function is called via a base class pointer to a derived 
  263. class object (the object can be of any derived class). The <tt><b>virtual</b></tt> functions 
  264. table (vtable) is used at run-time to enable the proper function to be called for the 
  265. object to which the base class pointer "points". Each class containing <tt><b>virtual</b></tt> 
  266. functions has its own vtable that specifies where the <tt><b>virtual</b></tt> functions for that 
  267. class are located. Every object of a class with <tt><b>virtual</b></tt> functions contains a 
  268. hidden pointer to the class's vtable. When a <tt><b>virtual</b></tt> function is called via a 
  269. base class pointer, the hidden pointer is dereferenced to locate the vtable, then the<br>
  270. <foreign  name="exercises" url="^Exercises::c:s0p5">
  271.  
  272. </page>
  273. <page pagename="Answer 10.6">
  274. vtable is searched for the proper function call.<br>
  275. <foreign  name="exercises" url="^Exercises::c:s0p5">
  276. <br>
  277.  
  278. </page>
  279. <page pagename="Answer 10.7">
  280. <b>Answer 10.7</b><br>
  281. When a class inherits implementation, it inherits previously defined functionality 
  282. from another class. When a class inherits interface, it inherits the definition of 
  283. what the interface to the new class type should be. The implementation is then 
  284. provided by the programmer defining the new class type. Inheritance hierarchies 
  285. designed for inheriting implementation are used to reduce the amont of new code 
  286. that is being written. Such hierarchies are used to facilitate software reusability. 
  287. Inheritance hierarchies designed for inheriting interface are used to write 
  288. programs that perform generic processing of many class types. Such hierarchies 
  289. are commonly used to facilitate software extensibility (i.e., new types can be 
  290. added to the hierarchy without changing the generic processing capabilitiesof the<br>
  291. <foreign  name="exercises" url="^Exercises::c:s0p6">
  292.  
  293. </page>
  294. <page pagename="Answer 10.7">
  295. program.)<br>
  296. <foreign  name="exercises" url="^Exercises::c:s0p6">
  297. <br>
  298.  
  299. </page>
  300. <page pagename="Answer 10.14">
  301. <b>Answer 10.14</b><br>
  302. The solution to this exercise can be found on your Cyber Classroom CD. Copy 
  303. the file cpphtp2/answers/P10_14.zip to your hard drive and unzip the program 
  304. code.<br>
  305. <foreign  name="exercises" url="^Exercises::c:s0p13">
  306. <br>
  307.  
  308. </page>
  309. </section>
  310. <section type=Popup name=Exercises title="Exercises">
  311. <page pagename="Exercise 10.1">
  312. <b>Exercise 10.1</b><br>
  313. Fill in the blanks in each of the following:<br>
  314. a)  Using inheritance and polymorphism helps eliminate ________ logic.<br>
  315. b)  A pure <b>virtual</b> function is specified by placing ________ at the end of its 
  316. prototype in the class definition.<br>
  317. c)  If a class contains one or more pure <b>virtual</b> functions, it is an ________.<br>
  318. d)  A function call resolved at compile time is referred to as ________ 
  319. binding.<br>
  320. e)  A function call resolved at run time is referred to as ________ binding.<br>
  321. <foreign  name="answers" url="^Answers::c:s0p0">
  322. <br>
  323. <br>
  324.  
  325. </page>
  326. <page pagename="Exercise 10.2">
  327. <b>Exercise 10.2</b><br>
  328. What are <b>virtual</b> functions? Describe a circumstance in which <b>virtual</b> functions 
  329. would be appropriate. <br>
  330. <br>
  331. <br>
  332.  
  333. </page>
  334. <page pagename="Exercise 10.3">
  335. <b>Exercise 10.3</b><br>
  336. Given that constructors cannot be <b>virtual</b>, describe a scheme for how you might 
  337. achieve a similar effect.<br>
  338. <br>
  339. <br>
  340.  
  341. </page>
  342. <page pagename="Exercise 10.4">
  343. <b>Exercise 10.4</b><br>
  344. How is it that polymorphism enables you to program "in the general" rather than 
  345. "in the specific." Discuss the key advantages of programming "in the general."<br>
  346. <br>
  347. <br>
  348.  
  349. </page>
  350. <page pagename="Exercise 10.5">
  351. <b>Exercise 10.5</b><br>
  352. Discuss the problems of programming with <b>switch</b> logic. Explain why 
  353. polymorphism is an effective alternative to using <b>switch</b> logic.<br>
  354. <br>
  355. <br>
  356.  
  357. </page>
  358. <page pagename="Exercise 10.6">
  359. <b>Exercise 10.6</b><br>
  360. Distinguish between static binding and dynamic binding. Explain the use of 
  361. <b>virtual</b> functions and the <i>vtable</i> in dynamic binding.<br>
  362. <foreign  name="answers" url="^Answers::c:s0p1">
  363. <br>
  364.  
  365. </page>
  366. <page pagename="Exercise 10.7">
  367. <b>Exercise 10.7</b><br>
  368. Distinguish between inheriting interface and inheriting implementation. How do 
  369. inheritance hierarchies designed for inheriting interface differ from those 
  370. designed for inheriting implementation?<br>
  371. <foreign  name="answers" url="^Answers::c:s0p3">
  372. <br>
  373.  
  374. </page>
  375. <page pagename="Exercise 10.8">
  376. <b>Exercise 10.8</b><br>
  377. Distinguish between <b>virtual</b> functions and pure <b>virtual</b> functions.<br>
  378. <br>
  379.  
  380. </page>
  381. <page pagename="Exercise 10.9">
  382. <b>Exercise 10.9</b><br>
  383. (True/False) All <b>virtual</b> functions in an abstract base class must be declared as 
  384. pure <b>virtual</b> functions.<br>
  385. <br>
  386. <br>
  387.  
  388. </page>
  389. <page pagename="Exercise 10.10">
  390. <b>Exercise 10.10</b><br>
  391. Suggest one or more levels of abstract base classes for the <b>Shape</b> hierarchy 
  392. discussed in this chapter (the first level is <b>Shape</b> and the second level consists of 
  393. the classes <b>TwoDimensionalShape</b> and <b>ThreeDimensionalShape</b>).<br>
  394. <br>
  395. <br>
  396.  
  397. </page>
  398. <page pagename="Exercise 10.11">
  399. <b>Exercise 10.11</b><br>
  400. How does polymorphism promote extensibility?<br>
  401. <br>
  402. <br>
  403.  
  404. </page>
  405. <page pagename="Exercise 10.12">
  406. <b>Exercise 10.12</b><br>
  407. You have been asked to develop a flight simulator that will have elaborate 
  408. graphical outputs. Explain why polymorphic programming would be especially 
  409. effective for a problem of this nature.<br>
  410. <br>
  411. <br>
  412.  
  413. </page>
  414. <page pagename="Exercise 10.13">
  415. <b>Exercise 10.13</b><br>
  416. Develop a basic graphics package. Use the <b>Shape</b> class inheritance hierarchy 
  417. from Chapter 9. Limit yourself to two-dimensional shapes such as squares, 
  418. rectangles, triangles and circles. Interact with the user. Let the user specify the 
  419. position, size, shape and fill characters to be used in drawing each shape. The 
  420. user can specify many items of the same shape. As you create each shape, place 
  421. a <b>Shape *</b> pointer to each new <b>Shape</b> object into an array. Each class has its own 
  422. draw member function. Write a polymorphic screen manager that walks 
  423. through the array (preferably using an iterator) sending <b>draw</b> messages to each 
  424. object in the array to form a screen image. Redraw the screen image each time 
  425. the user specifies an additional shape.<br>
  426. <br>
  427.  
  428. </page>
  429. <page pagename="Exercise 10.14">
  430. <b>Exercise 10.14</b><br>
  431. Modify the payroll system of <a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 10.1</a> to add private data members <b>birthDate</b> 
  432. (a <b>Date</b> object) and <b>departmentCode</b> (an <b>int</b>) to class <b>Employee</b>. Assume this 
  433. payroll is processed once per month. Then, as your program calculates the 
  434. payroll for each <b>Employee</b> (polymorphically), add a $100.00 bonus to the 
  435. person's payroll amount if this is the month in which the <b>Employee</b>'s birthday 
  436. occurs.<br>
  437. <foreign  name="answers" url="^Answers::c:s0p5">
  438.  
  439. </page>
  440. <page pagename="Exercise 10.15">
  441. <b>Exercise 10.15</b><br>
  442. In Exercise 9.14, you developed a <b>Shape</b> class hierarchy and defined the classes 
  443. in the hierarchy. Modify the hierarchy so that class <b>Shape</b> is an abstract base 
  444. class containing the interface to the hierarchy. Derive <b>TwoDimensionalShape</b> 
  445. and <b>ThreeDimensionalShape</b> from class <b>Shape</b>--these classes should also be 
  446. abstract. Use a <b>virtual print</b> function to output the type and dimensions of each 
  447. class. Also include <b>virtual area</b> and <b>volume</b> functions so these calculations can 
  448. be performed for objects of each concrete class in the hierarchy. Write a driver 
  449. program that tests the <b>Shape</b> class hierarchy.<br>
  450. <br>
  451. <br>
  452.  
  453. </page>
  454. </section>
  455. <section type=Popup name=Perform title="Performance">
  456. <page>
  457. Polymorphism as 
  458. implemented with 
  459. <b>virtual</b> functions and 
  460. dynamic binding is 
  461. efficient. Programmers 
  462. may use these 
  463. capabilities with 
  464. nominal impact on 
  465. system performance. <br>
  466. <br>
  467.  
  468. </page>
  469. <page>
  470. Virtual functions and 
  471. dynamic binding enable 
  472. polymorphic 
  473. programming as 
  474. opposed to <b>switch</b> logic 
  475. programming. C++ 
  476. optimizing compilers 
  477. normally generate code 
  478. that runs at least as 
  479. efficiently as hand-<br>
  480.  
  481. </page>
  482. <page>
  483. coded <b>switch</b>-based 
  484. logic. One way or the 
  485. other, the overhead of 
  486. polymorphism is 
  487. acceptable for most 
  488. applications. But in 
  489. some situations--real-
  490. time applications with 
  491. stringent performance 
  492. requirements, for <br>
  493.  
  494. </page>
  495. <page>
  496. example--the overhead 
  497. of polymorphism may 
  498. be too high.<br>
  499. <br>
  500.  
  501. </page>
  502. </section>
  503. <section type=Popup name=Code title="Code Examples">
  504. <page>
  505. <font size=18>Figure 10.1  Demonstrating polymorphism with the <b>Employee</b> class hierarchy.</font><br>
  506. <applet code=gsl.win.helper.TextBox width=580 height=300>
  507. <param  name="file" value="code/ch10/fig10_01.txt">
  508. </applet><br>
  509. <br>
  510. <foreign  name="copy2disk" url="!jcpy Source/fig10_01.jar">
  511. <foreign  name="audio" url="~audio/Ch10/10fig001.au">
  512. <foreign  name="execute" url="!jarexe Run/fig10_01.jar">
  513. <br>
  514.  
  515. </page>
  516. <page>
  517. <font size=18>Figure 10.2  Definition of abstract base class <b>Shape</b>.</font><br>
  518. <applet code=gsl.win.helper.TextBox width=580 height=300>
  519. <param  name="file" value="code/ch10/fig10_02.txt">
  520. </applet><br>
  521. <br>
  522. <foreign  name="copy2disk" url="!jcpy Source/fig10_02.jar">
  523. <foreign  name="audio" url="~audio/Ch10/10fig002.au">
  524. <foreign  name="execute" url="!jarexe Run/fig10_02.jar">
  525. <br>
  526.  
  527. </page>
  528. </section>
  529. <section type=Popup name=Practice title="Good Practices">
  530. <page>
  531. Even though certain 
  532. functions are implicitly 
  533. <b>virtual</b> because of a 
  534. declaration made 
  535. higher in the class 
  536. hierarchy, explicitly 
  537. declare these functions 
  538. <b>virtual</b> at every level of 
  539. the hierarchy to <br>
  540.  
  541. </page>
  542. <page>
  543. promote program 
  544. clarity.<br>
  545. <br>
  546.  
  547. </page>
  548. <page>
  549. If a class has <b>virtual</b> 
  550. functions, provide a 
  551. <b>virtual</b> destructor even 
  552. if one is not required 
  553. for the class. Classes 
  554. derived from this class 
  555. may contain destructors 
  556. that must be called 
  557. properly. <br>
  558. <br>
  559.  
  560. </page>
  561. </section>
  562. <section type=Popup name=Objective title="Objectives">
  563. <page>
  564. <indent width=8 delay>*   To understand the notion of polymorphism.</indent>
  565. <indent width=8 delay>*   To understand how to declare and use <b>virtual</b> functions to effect polymorphism.</indent>
  566. <indent width=8 delay>*   To understand the distinction between abstract classes and concrete 
  567. classes.</indent>
  568. <foreign  name="audio" url="~audio/Ch10/10obj.au">
  569.  
  570. </page>
  571. <page>
  572. <indent width=8 delay>*   To learn how to declare pure <b>virtual</b> functions to create abstract classes.</indent>
  573. <indent width=8 delay>*   To appreciate how polymorphism makes systems extensible and maintainable.</indent>
  574. <indent width=8 delay>*   To understand how C++ implements <b>virtual</b> functions and dynamic binding "under the hood."</indent>
  575. <br>
  576.  
  577. </page>
  578. </section>
  579. <section type=Popup name=Errors title="Common Errors">
  580. <page>
  581. Attempting to 
  582. instantiate an object of 
  583. an abstract class (i.e., a 
  584. class that contains one 
  585. or more pure <b>virtual</b> 
  586. functions) is a syntax 
  587. error.<br>
  588. <br>
  589.  
  590. </page>
  591. <page>
  592. Constructors cannot be 
  593. <b>virtual</b>. Declaring a 
  594. constructor as a <b>virtual</b> 
  595. function is a syntax 
  596. error. <br>
  597. <br>
  598.  
  599. </page>
  600. </section>
  601. <section type=Popup name=Engineer title="Engineering">
  602. <page>
  603. An interesting 
  604. consequence of using 
  605. <b>virtual</b> functions and 
  606. polymorphism is that 
  607. programs take on a 
  608. simplified appearance. 
  609. They contain less 
  610. branching logic in favor 
  611. of simpler sequential 
  612. code. This facilitates <br>
  613.  
  614. </page>
  615. <page>
  616. testing, debugging, 
  617. program maintenance 
  618. and bug avoidance. <br>
  619. <br>
  620.  
  621. </page>
  622. <page>
  623. Once a function is 
  624. declared <b>virtual</b>, it 
  625. remains <b>virtual</b> all the 
  626. way down the 
  627. inheritance hierarchy 
  628. from that point even if 
  629. it is not declared 
  630. <b>virtual</b> when a class 
  631. overrides it. <br>
  632. <br>
  633.  
  634. </page>
  635. <page>
  636. When a derived class 
  637. chooses not to define a 
  638. <b>virtual</b> function, the 
  639. derived class simply 
  640. inherits its immediate 
  641. base class's <b>virtual</b> 
  642. function definition.<br>
  643. <br>
  644.  
  645. </page>
  646. <page>
  647. If a class is derived 
  648. from a class with a pure 
  649. <b>virtual</b> function, and if 
  650. no definition is 
  651. supplied for that pure 
  652. <b>virtual</b> function in the 
  653. derived class, then that 
  654. <b>virtual</b> function 
  655. remains pure in the 
  656. derived class. <br>
  657.  
  658. </page>
  659. <page>
  660. Consequently, the 
  661. derived class is also an 
  662. abstract class. <br>
  663. <br>
  664.  
  665. </page>
  666. <page>
  667. An abstract class 
  668. defines an interface for 
  669. the various members of 
  670. a class hierarchy. The 
  671. abstract class contains 
  672. pure <b>virtual</b> functions 
  673. that will be defined in 
  674. the derived classes. All 
  675. functions in the 
  676. hierarchy can use this <br>
  677.  
  678. </page>
  679. <page>
  680. same interface through 
  681. polymorphism.<br>
  682. <br>
  683.  
  684. </page>
  685. <page>
  686. Polymorphism 
  687. promotes extensibility: 
  688. Software written to 
  689. invoke polymorphic 
  690. behavior is written 
  691. independently of the 
  692. types of the objects to 
  693. which messages are 
  694. sent. Thus, new types 
  695. of objects that can <br>
  696.  
  697. </page>
  698. <page>
  699. respond to existing 
  700. messages can be added 
  701. into such a system 
  702. without modifying the 
  703. base system. Except for 
  704. client code that 
  705. instantiates new 
  706. objects, programs need 
  707. not be recompiled.<br>
  708. <br>
  709.  
  710. </page>
  711. <page>
  712. With <b>virtual</b> functions 
  713. and polymorphism, the 
  714. programmer can deal in 
  715. generalities and let the 
  716. execution-time 
  717. environment concern 
  718. itself with the specifics. 
  719. The programmer can 
  720. command a wide 
  721. variety of objects to <br>
  722.  
  723. </page>
  724. <page>
  725. behave in manners 
  726. appropriate to those 
  727. objects without even 
  728. knowing the types of 
  729. those objects. <br>
  730. <br>
  731.  
  732. </page>
  733. <page>
  734. A class can inherit 
  735. interface and/or 
  736. implementation from a 
  737. base class. Hierarchies 
  738. designed for 
  739. implementation 
  740. inheritance tend to have 
  741. their functionality high 
  742. in the hierarchy--each 
  743. new derived class <br>
  744.  
  745. </page>
  746. <page>
  747. inherits one or more 
  748. member functions that 
  749. were defined in a base 
  750. class, and the new 
  751. derived class uses the 
  752. base-class definitions. 
  753. Hierarchies designed 
  754. for interface inheritance 
  755. tend to have their 
  756. functionality lower in <br>
  757.  
  758. </page>
  759. <page>
  760. the hierarchy--a base 
  761. class specifies one or 
  762. more functions that 
  763. should be called 
  764. identically for each 
  765. object in the hierarchy 
  766. (i.e., they have the 
  767. same signature), but the 
  768. individual derived 
  769. classes provide their <br>
  770.  
  771. </page>
  772. <page>
  773. own implementations 
  774. of the function(s). <br>
  775. <br>
  776.  
  777. </page>
  778. </section>
  779. <section type=Body name=Default title="10 Virtual Functions and Polymorphism">
  780. <page>
  781. <font size=18 bold>10 Virtual Functions and Polymorphism</font><hr>
  782. <a href="#s1p0">10.1<spacer width=20 height=1>Introduction</a>  <br>
  783. <a href="#s2p0">10.2<spacer width=20 height=1>Type Fields and switch Statements</a>  <br>
  784. <a href="#s3p0">10.3<spacer width=20 height=1>Virtual Functions</a>  <br>
  785. <a href="#s4p0">10.4<spacer width=20 height=1>Abstract Base Classes and Concrete Classes</a>  <br>
  786. <a href="#s5p0">10.5<spacer width=20 height=1>Polymorphism</a>  <br>
  787. <a href="#s6p0">10.6<spacer width=20 height=1>Case Study: A Payroll System Using 
  788. Polymorphism</a>  <br>
  789. <a href="#s7p0">10.7<spacer width=20 height=1>New Classes and Dynamic Binding</a>  <br>
  790. <a href="#s8p0">10.8<spacer width=20 height=1>Virtual Destructors</a>  <br>
  791. <foreign  name="objectivesButton" url="^Objective::c:s0p0">
  792. <foreign  name="quotes" url="^Quotes::c:s0p0">
  793.  
  794. </page>
  795. <page>
  796. <a href="#s9p0">10.9<spacer width=20 height=1>Case Study: Inheriting Interface and 
  797. Implementation</a>  <br>
  798. <a href="#s10p0">10.10<spacer width=20 height=1>Polymorphism, virtual Functions and Dynamic 
  799. Binding "Under the Hood"</a>  <br>
  800. <a href="#s11p0">10.11<spacer width=20 height=1>Summary</a>  <br>
  801. <a href="^Terminology::c:s0p0">Terminology</a>  <br>
  802. <a href="^Illustration::c:s0p0">Figures</a>  <br>
  803. <foreign  name="objectivesButton" url="^Objective::c:s0p0">
  804. <foreign  name="quotes" url="^Quotes::c:s0p0">
  805.  
  806. </page>
  807. </section>
  808. <section type=Body name=Default title="10.1 Introduction">
  809. <page>
  810. <font size=18 bold>10.1 Introduction</font><hr>
  811. With <i>virtual functions</i> and <i>polymorphism</i>, it is possible 
  812. to design and implement systems that are more easily 
  813. <i>extensible</i>. Programs can be written to generically 
  814. process--as base-class objects--objects of all existing 
  815. classes in a hierarchy. Classes that do not exist during 
  816. program development can be added with little or no 
  817. modifications to the generic part of the program--as 
  818. long as those classes are part of the hierarchy that is 
  819. being processed generically. The only parts of a 
  820. program that will need modification are those parts that <br>
  821.  
  822. </page>
  823. <page>
  824. require direct knowledge of the particular class that is 
  825. added to the hierarchy.<br>
  826.  
  827. </page>
  828. </section>
  829. <section type=Body name=Default title="10.2 Type Fields and switch Statements">
  830. <page>
  831. <font size=18 bold>10.2 Type Fields and <tt>switch</tt> Statements</font><hr>
  832. One means of dealing with objects of different types is 
  833. to use a <b>switch</b> statement to take an appropriate action 
  834. on each object based on that object's type. For example, 
  835. in a hierarchy of shapes in which each shape specifies 
  836. its type as a data member, a <b>switch</b> structure could 
  837. determine which <b>print</b> function to call based on the 
  838. type of the particular object. <br>
  839. <spacer width=16 height=1>There are many problems with using <b>switch</b> logic. The 
  840. programmer might forget to make such a type test when 
  841. one is warranted. The programmer may forget to test all 
  842. possible cases in a <b>switch</b>. If a <b>switch</b>-based system is <br>
  843.  
  844. </page>
  845. <page>
  846. modified by adding new types, the programmer might 
  847. forget to insert the new cases in all existing <b>switch</b> 
  848. statements. Every addition or deletion of a class to 
  849. handle new types demands that every <b>switch</b> statement 
  850. in the system be modified; tracking these down can be 
  851. time consuming and error-prone. <br>
  852. <spacer width=16 height=1><a href="^Engineer::c:s0p0"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>As we will see, <b>virtual</b> functions and polymorphic 
  853. programming can eliminate the need for <b>switch</b> logic. 
  854. The programmer can use the <b>virtual</b> function 
  855. mechanism to perform the equivalent logic 
  856. automatically, thus avoiding the kinds of errors 
  857. typically associated with <b>switch</b> logic.<br>
  858.  
  859. </page>
  860. </section>
  861. <section type=Body name=Default title="10.3 Virtual Functions">
  862. <page>
  863. <font size=18 bold>10.3 Virtual Functions</font><hr>
  864. Suppose a set of shape classes such as <b>Circle</b>, <b>Triangle</b>, 
  865. <b>Rectangle</b>, <b>Square</b>, etc. are all derived from base class 
  866. <b>Shape</b>. In object-oriented programming, each of these 
  867. classes might be endowed with the ability to draw itself. 
  868. Although each class has its own <b>draw</b> function, the 
  869. <b>draw</b> function for each shape is quite different. When 
  870. drawing a shape, whatever that shape may be, it would 
  871. be nice to be able to treat all these shapes generically as 
  872. objects of the base class <b>Shape</b>. Then to draw any 
  873. shape, we could simply call function <b>draw</b> of base class 
  874. <b>Shape</b> and let the program determine <i>dynamically</i> (i.e., <br>
  875.  
  876. </page>
  877. <page>
  878. at execution time) which derived class <b>draw</b> function to 
  879. use. <br>
  880. <spacer width=16 height=1><a href="^Engineer::c:s0p2"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>To enable this kind of behavior, we declare draw in the 
  881. base class as a <b><i>virtual function</i></b> and we <i>override</i> <b>draw</b> in 
  882. each of the derived classes to draw the appropriate 
  883. shape.  <a href="^Practice::c:s0p0"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>A virtual function is declared by preceding the 
  884. function's prototype with the keyword <b>virtual</b> in the 
  885. base class. For example, <br>
  886. <font size=2><br></font><font size=11><pre>
  887. virtual void draw() const;<p>
  888. </pre></font>
  889. may appear in base-class <b>Shape</b>. The preceding 
  890. prototype declares that function <b>draw</b> is a constant 
  891. function that takes no arguments, returns nothing and is 
  892. a <b>virtual</b> <b><a href="^Engineer::c:s0p3"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a></b>function. <br>
  893.  
  894. </page>
  895. <page>
  896. If function <b>draw</b> in the base class has been declared 
  897. <b>virtual</b>, and if we then use a base-class pointer or 
  898. reference to point to the derived-class object and invoke 
  899. the <b>draw</b> function using this pointer (e.g., <b>shapePtr-
  900. >draw()</b>) or reference, the program will choose the 
  901. correct derived class's <b>draw</b> function dynamically (i.e., 
  902. at execution time). Such <i>dynamic binding</i> will be 
  903. illustrated in the case studies in<a href="#s6p0"> Sections 10.6</a> and<a href="#s9p0"> 10.9</a>.<br>
  904. <spacer width=16 height=1>When a <b>virtual</b> function is called by referencing a 
  905. specific object by name and using the dot member 
  906. selection operator (e.g., <b>squareObject.draw()</b>), the 
  907. reference is resolved at compile time (this is called 
  908. <i>static binding</i>) and the <b>virtual</b> function that is called is <br>
  909.  
  910. </page>
  911. <page>
  912. the one defined for (or inherited by) the class of that 
  913. particular object.<br>
  914.  
  915. </page>
  916. <page>
  917. <b>Select the true statement(s). </b><br>
  918. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  919. A virtual function is declared with the virtual keyword preceding the function name in the base class.  <br>
  920. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  921. Dynamic binding is the process by which a function call through a base class pointer is resolved at runtime to determine which derived class virtual function to invoke. <br>
  922. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Static binding occurs at compile-time if a specific object invokes the virtual function.">
  923. Dynamic binding is always used for virtual functions.   <br>
  924. <component type=button name=b label="Check Your Answer" width=125 height=24>
  925.  
  926. </page>
  927. </section>
  928. <section type=Body name=Default title="10.4 Abstract Base Classes and Concrete Classes">
  929. <page>
  930. <font size=18 bold>10.4 Abstract Base Classes and Concrete 
  931. Classes</font><hr>
  932. When we think of a class as a type, we assume that 
  933. objects of that type will be instantiated. However, there 
  934. are cases in which it is useful to define classes for 
  935. which the programmer never intends to instantiate any 
  936. objects. Such classes are called <i>abstract classes</i>. 
  937. Because these are used as base classes in inheritance 
  938. situations, we normally will refer to them as <i>abstract 
  939. base classes</i>. No objects of an abstract base class can be 
  940. instantiated.<br>
  941.  
  942. </page>
  943. <page>
  944. The sole purpose of an abstract class is to provide an 
  945. appropriate base class from which classes may inherit 
  946. interface and/or implementation. Classes from which 
  947. objects can be instantiated are called <i>concrete classes</i>.<br>
  948. <spacer width=16 height=1>We could have an abstract base class 
  949. <b>TwoDimensionalShape</b> and derive concrete classes 
  950. such as <b>Square</b>, <b>Circle</b>, <b>Triangle</b>, etc. We could also 
  951. have an abstract base class <b>ThreeDimensionalShape</b> 
  952. and derive concrete classes such as <b>Cube</b>, <b>Sphere</b>, 
  953. <b>Cylinder</b>, etc. <a href="^Errors::c:s0p0"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>Abstract base classes are too generic to 
  954. define real objects; we need to be more specific before 
  955. we can think of instantiating objects. That is what <br>
  956.  
  957. </page>
  958. <page>
  959. concrete classes do; they provide the specifics that 
  960. make it reasonable to instantiate objects.<br>
  961. <spacer width=16 height=1><a href="^Engineer::c:s0p4"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>A class is made abstract by declaring one or more of its 
  962. <tt><b>virtual</b></tt> functions to be "pure." A <i>pure</i> <b><i>virtual</i></b> 
  963. <i>function</i> is one with an <i>initializer of <tt><b>=</b></tt> <tt><b>0</b></tt></i> in its 
  964. declaration as in<br>
  965. <font size=2><br></font><font size=11><pre>
  966. virtual float earnings() const = 0;   // pure virtual <p>
  967. </pre></font>
  968. A hierarchy does not need to contain any abstract 
  969. classes, but as we will see, many good object-oriented 
  970. systems have class hierarchies headed by an abstract 
  971. base class. In some cases, abstract classes constitute the 
  972. top few levels of the hierarchy. A good example of this <br>
  973.  
  974. </page>
  975. <page>
  976. is a shape hierarchy. The hierarchy could be headed by 
  977. abstract base class <b>Shape</b>. On the next level down, we 
  978. can have two more abstract base classes, namely 
  979. <b>TwoDimensionalShape</b> and 
  980. <b>ThreeDimensionalShape</b>. The next level down would 
  981. start defining concrete classes for two-dimensional 
  982. shapes such as circles and squares, and concrete classes 
  983. for three-dimensional shapes such as spheres and cubes.<br>
  984.  
  985. </page>
  986. <page>
  987. <b>Select the true statement(s). </b><br>
  988. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  989. Objects of an abstract base class cannot be instantiated.  <br>
  990. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  991. Abstract base classes can be used for inheritance. <br>
  992. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Concrete classes are classes from which objects can be instantiated (i.e., non-abstract classes).">
  993. Concrete classes are classes that cannot be used as base classes.   <br>
  994. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  995. An abstract base class contains one or more "pure" virtual functions. <br>
  996. <component type=button name=b label="Check Your Answer" width=125 height=24>
  997.  
  998. </page>
  999. </section>
  1000. <section type=Body name=Default title="10.5 Polymorphism">
  1001. <page>
  1002. <font size=18 bold>10.5 Polymorphism</font><hr>
  1003. C++ enables <i>polymorphism</i>--the ability for objects of 
  1004. different classes related by inheritance to respond 
  1005. differently to the same message (i.e., member function 
  1006. call). The same message sent to many different types of 
  1007. objects takes on "many forms"--hence the term 
  1008. polymorphism. If, for example, class <b>Rectangle</b> is 
  1009. derived from class <b>Quadrilateral</b>, then a Rectangle 
  1010. object <i>is a</i> more specific version of a <b>Quadrilateral</b> 
  1011. object. An operation (such as calculating the perimeter 
  1012. or the area) that can be performed on an <b>Quadrilateral</b> 
  1013. object also can be performed on a <b>Rectangle</b> object <br>
  1014.  
  1015. </page>
  1016. <page>
  1017. Polymorphism is implemented via <b>virtual</b> functions. 
  1018. When a request is made through a base-class pointer (or 
  1019. reference) to use a <b>virtual</b> function, C++ chooses the 
  1020. correct overridden function in the appropriate derived 
  1021. class associated with the object.<br>
  1022. <spacer width=16 height=1>Sometimes a non-<b>virtual</b> member function is defined in 
  1023. a base class and overridden in a derived class. If such a 
  1024. member function is called through a base-class pointer 
  1025. to the derived class object, the base-class version is 
  1026. used. If the member function is called through a 
  1027. derived-class pointer, the derived-class version is used. 
  1028. This is nonpolymorphic behavior. <br>
  1029.  
  1030. </page>
  1031. <page>
  1032. Consider the following example using the <b>Employee</b> 
  1033. base class and <b>HourlyWorker</b> derived class of Fig. 9.5:<br>
  1034. <font size=2><br></font><font size=11><pre>
  1035. Employee e, *ePtr = &e;<p>
  1036. HourlyWorker h, *hPtr = &h;<p>
  1037. ePtr->print();     // call base-class print function<p>
  1038. hPtr->print();     // call derived-class print function<p>
  1039. ePtr = &h;         // allowable implicit conversion<p>
  1040. ePtr->print();     // still calls base-class print<p>
  1041. </pre></font>
  1042. Our <b>Employee</b> base class and <b>HourlyWorker</b> derived 
  1043. class both have their own <b>print</b> functions defined. 
  1044. Because the functions were not declared <b>virtual</b> and 
  1045. they have the same signature, calling the <b>print</b> function <br>
  1046.  
  1047. </page>
  1048. <page>
  1049. through an <b>Employee</b> pointer results in 
  1050. <b>Employee::print()</b> being called (regardless of whether 
  1051. the <b>Employee</b> pointer is pointing to a base-class 
  1052. <b>Employee</b> object or a derived-class <b>HourlyWorker</b> 
  1053. object) and calling the <b>print</b> function through an 
  1054. <b>HourlyWorker</b> pointer results in 
  1055. <b>HourlyWorker::print()</b> being called. The base-class 
  1056. <b>print</b> function is also available to the derived class, but 
  1057. to call the base-class <b>print</b> for a derived-class object 
  1058. through a pointer to a derived-class object, for example, 
  1059. the function must be called explicitly as follows:<br>
  1060. <font size=2><br></font><font size=11><pre>
  1061. hPtr->Employee::print();  // call base-class print function<p>
  1062. </pre></font>
  1063.  
  1064. </page>
  1065. <page>
  1066. This specifies that the base-class <b>print</b> should be called 
  1067. explicitly.<br>
  1068. <spacer width=16 height=1>Through the use of <b>virtual</b> functions and 
  1069. polymorphism, one member function call can cause 
  1070. different actions to occur depending on the type of the 
  1071. object receiving the call (we will see that a small 
  1072. amount of execution-time overhead is required). <a href="^Engineer::c:s0p10"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>This 
  1073. gives the programmer tremendous expressive 
  1074. capability. <a href="^Engineer::c:s0p8"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>We will see examples of the power of 
  1075. polymorphism and <b>virtual</b> functions in the next  <a href="^Engineer::c:s0p6"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>several 
  1076. sections.<br>
  1077. <spacer width=16 height=1>Although we cannot instantiate objects of abstract base 
  1078. classes, we <i>can</i> declare pointers and references to <br>
  1079.  
  1080. </page>
  1081. <page>
  1082. abstract base classes. Such pointers and references can 
  1083. then be used to enable polymorphic manipulations of 
  1084. derived-class objects when such objects are instantiated 
  1085. from concrete classes.<br>
  1086. <spacer width=16 height=1>Let us consider applications of polymorphism and 
  1087. <b>virtual</b> functions. A screen manager needs to display a 
  1088. variety of objects of different classes, including new 
  1089. types of objects that will be added to the system even 
  1090. after the screen manager is written. The system may 
  1091. need to display various shapes (i.e., the base class is 
  1092. <b>Shape</b>) such as squares, circles, triangles, rectangles, 
  1093. points, lines and the like (each of these shape classes is 
  1094. derived from the base class <b>Shape</b>). The screen <br>
  1095.  
  1096. </page>
  1097. <page>
  1098. manager uses base-class pointers or references (to 
  1099. <b>Shape</b>) to manage all the objects to be displayed. To 
  1100. draw any object (regardless of the level at which that 
  1101. object appears in the inheritance hierarchy), the screen 
  1102. manager uses a base-class pointer (or reference) to the 
  1103. object and simply sends a <b>draw</b> message to the object. 
  1104. Function <b>draw</b> has been declared pure <b>virtual</b> in base 
  1105. class <b>Shape</b> and has been overridden in each of the 
  1106. derived classes. Each <b>Shape</b> object knows how to draw 
  1107. itself. The screen manager does not have to worry about 
  1108. what type each object is or whether the object is of a 
  1109. type the screen manager has seen before--the screen 
  1110. manager simply tells each object to <b>draw</b> itself.<br>
  1111.  
  1112. </page>
  1113. <page>
  1114. Polymorphism is particularly effective for 
  1115. implementing layered software systems. In operating 
  1116. systems, for example, each type of physical device may 
  1117. operate quite differently from the others. Regardless of 
  1118. this, commands to <i>read</i> or <i>write</i> data from and to 
  1119. devices can have a certain uniformity. The <i>write</i> 
  1120. message sent to a device-driver object needs to be 
  1121. interpreted specifically in the context of that device 
  1122. driver and how that device driver manipulates devices 
  1123. of a specific type. However, the <i>write</i> call itself is really 
  1124. no different from the <i>write</i> to any other device in the 
  1125. system--it simply places some number of bytes from 
  1126. memory onto that device. An object-oriented operating <br>
  1127.  
  1128. </page>
  1129. <page>
  1130. system might use an abstract base class to provide an 
  1131. interface appropriate for all device drivers. Then, 
  1132. through inheritance from that abstract base class, 
  1133. derived classes are formed that all operate similarly. 
  1134. The capabilities (i.e., the <b>public</b> interface) offered by 
  1135. the device drivers are provided as pure <b>virtual</b> 
  1136. functions in the abstract base class. Implementations of 
  1137. these <b>virtual</b> functions are provided in the derived 
  1138. classes that correspond to the specific types of device 
  1139. drivers.<br>
  1140. <spacer width=16 height=1>With polymorphic programming, a program might walk 
  1141. through an array of pointers to objects from various 
  1142. levels of a class hierarchy. The pointers in such an array <br>
  1143.  
  1144. </page>
  1145. <page>
  1146. would all be base-class pointers to derived-class 
  1147. objects. For example, an array of objects of class 
  1148. <b>TwoDimensionalShape</b> could contain 
  1149. <b>TwoDimensionalShape *</b> pointers to objects from the 
  1150. derived classes <b>Square</b>, <b>Circle</b>, <b>Triangle</b>, <b>Rectangle</b>, 
  1151. <b>Line</b>, etc. Sending a message to draw each object in the 
  1152. array would, using polymorphism, draw the correct 
  1153. picture on the screen. <br>
  1154.  
  1155. </page>
  1156. <page>
  1157. <b>Select the true statement(s). </b><br>
  1158. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1159. C++ implements polymorphism through virtual functions.  <br>
  1160. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1161. Through the use of virtual functions and polymorphism, one member function call can cause different actions to occur depending on the type of the object receiving the call. <br>
  1162. <component type="checkbox" width=20 height=18 label="" name=""  feedback=" False. Polymorphism is referred to as "programming in the general."">
  1163. Polymorphism can be thought of as "programming in the specific."  <br>
  1164. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1165.  
  1166. </page>
  1167. </section>
  1168. <section type=Body name=Default title="10.6 Case Study: A Payroll System Using Polymorphism">
  1169. <page>
  1170. <font size=18 bold>10.6 Case Study: A Payroll System Using 
  1171. Polymorphism</font><hr>
  1172. Let us use <b>virtual</b> functions and polymorphism to 
  1173. perform payroll calculations based on the type of an 
  1174. employee (<a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 10.1</a>). We use a base class <b>Employee</b>. 
  1175. The derived classes of <b>Employee</b> are <b>Boss</b> who gets 
  1176. paid a fixed weekly salary regardless of the number of 
  1177. hours worked, <b>CommissionWorker</b> who gets a flat 
  1178. base salary plus a percentage of sales, <b>PieceWorker</b> 
  1179. who gets paid by the number of items produced, and 
  1180. <b>HourlyWorker</b> who gets paid by the hour and receives 
  1181. overtime pay. <br>
  1182.  
  1183. </page>
  1184. <page>
  1185. An <b>earnings</b> function call certainly applies generically 
  1186. to all employees. But the way each person's earnings 
  1187. are calculated depends on the class of the employee, 
  1188. and these classes are all derived from the base class 
  1189. <b>Employee</b>. So <b>earnings</b> is declared pure <b>virtual</b> in base 
  1190. class <b>Employee</b> and appropriate implementations of 
  1191. <b>earnings</b> are provided for each of the derived classes. 
  1192. Then, to calculate any employee's earnings, the 
  1193. program simply uses a base-class pointer (or reference) 
  1194. to that employee's object and invokes the <b>earnings</b> 
  1195. function. In a real payroll system, the various employee 
  1196. objects might be pointed to by individual elements in an 
  1197. array (or list) of pointers of type <b>Employee *</b>. The <br>
  1198.  
  1199. </page>
  1200. <page>
  1201. program would simply walk through the array one 
  1202. element at a time using the <b>Employee *</b> pointers to 
  1203. invoke the <b>earnings</b> function of each object.<br>
  1204. <spacer width=16 height=1>Let us consider the <b>Employee</b> class (<a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 10.1</a>, parts 1 
  1205. and 2). The <b>public</b> member functions include a 
  1206. constructor that takes the first name and last name as 
  1207. arguments; a destructor that reclaims dynamically 
  1208. allocated memory; a <i>get</i> function that returns the first 
  1209. name; a <i>get</i> function that returns the last name; a pure 
  1210. <b>virtual</b> function <b>earnings</b>; and a <b>virtual</b> function <b>print</b>. 
  1211. Why is <b>earnings</b> pure <b>virtual</b>? The answer is that it 
  1212. does not make sense to provide an implementation of 
  1213. this function in the <b>Employee</b> class. We cannot <br>
  1214.  
  1215. </page>
  1216. <page>
  1217. calculate the earnings for a generic employee--we must 
  1218. first know what kind of employee it is. By making this 
  1219. function pure <b>virtual</b> we are indicating that we will 
  1220. provide an implementation of this function in each 
  1221. derived class, but not in the base class itself. The 
  1222. programmer never intends to call this pure <b>virtual</b> 
  1223. function in the abstract base class <b>Employee</b>; all 
  1224. derived classes will override <b>earnings</b> with appropriate 
  1225. implementations for those classes.<br>
  1226. <spacer width=16 height=1>Class <b>Boss</b> (<a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 10.1</a>, parts 3 and 4) is derived from 
  1227. <b>Employee</b> with <b>public</b> inheritance. The <b>public</b> member 
  1228. functions include a constructor that takes a first name, a 
  1229. last name and a weekly salary as arguments, and passes <br>
  1230.  
  1231. </page>
  1232. <page>
  1233. the first name and last name to the <b>Employee</b> 
  1234. constructor to initialize the <b>firstName</b> and <b>lastName</b> 
  1235. members of the base-class part of the derived-class 
  1236. object; a <i>set</i> function to assign a new value to <b>private</b> 
  1237. data member <b>weeklySalary</b>; a virtual <b>earnings</b> 
  1238. function defining how to calculate a <b>Boss</b>'s earnings; 
  1239. and a <b>virtual print</b> function that outputs the type of the 
  1240. employee then calls <b>Employee::print()</b> to output the 
  1241. employee's name.<br>
  1242. <spacer width=16 height=1>Class <b>CommissionWorker</b> (<a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 10.1</a>, parts 5 and 6) is 
  1243. derived from <b>Employee</b> with <b>public</b> inheritance. The 
  1244. <b>public</b> member functions include a constructor that 
  1245. takes a first name, a last name, a salary, a commission <br>
  1246.  
  1247. </page>
  1248. <page>
  1249. and a quantity of items sold as arguments, and passes 
  1250. the first name and last name to the <b>Employee</b> 
  1251. constructor; <i>set</i> functions to assign new values to 
  1252. <b>private</b> data members <b>salary</b>, <b>commission</b> and 
  1253. <b>quantity</b>; a <b>virtual earnings</b> function defining how to 
  1254. calculate a <b>CommissionWorker</b>'s earnings; and a 
  1255. <b>virtual print</b> function that outputs the type of the 
  1256. employee then calls <b>Employee::print(<tt>)</tt></b> to output the 
  1257. employee's name.<br>
  1258. <spacer width=16 height=1>Class <b>PieceWorker</b> (<a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 10.1</a>, parts 7, 8 and 9) is 
  1259. derived from <b>Employee</b> with <b>public</b> inheritance. The 
  1260. <b>public</b> member functions include a constructor that 
  1261. takes a first name, a last name, a wage per piece and a <br>
  1262.  
  1263. </page>
  1264. <page>
  1265. quantity of items produced as arguments, and passes the 
  1266. first name and last name to the <b>Employee</b> constructor; 
  1267. set functions to assign new values to private data 
  1268. members <b>wagePerPiece</b> and <b>quantity</b>; a <b>virtual 
  1269. earnings</b> function defining how to calculate a 
  1270. <b>PieceWorker</b>'s earnings; and a <b>virtual print</b> function 
  1271. that outputs the type of the employee then calls 
  1272. <b>Employee::print()</b> to output the employee's name.<br>
  1273. <spacer width=16 height=1>Class <b>HourlyWorker</b> (<a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 10.1</a>, parts 10 and 11) is 
  1274. derived from <b>Employee</b> with <b>public</b> inheritance. The 
  1275. <b>public</b> member functions include a constructor that 
  1276. takes a first name, a last name, a wage and the number 
  1277. of hours worked as arguments and passes the first name <br>
  1278.  
  1279. </page>
  1280. <page>
  1281. and last name to the <b>Employee</b> constructor to initialize 
  1282. the <b>firstName</b> and <b>lastName</b> members of the base-class 
  1283. part of the derived-class object; <i>set</i> functions to assign 
  1284. new values to <b>private</b> data members <b>wage</b> and <b>hours</b>; a 
  1285. <b>virtual earnings</b> function defining how to calculate an 
  1286. <b>HourlyWorker</b>'s earnings; and a <b>virtual print</b> 
  1287. function that outputs the type of the employee then calls 
  1288. <b>Employee::print()</b> to output the employee's name.<br>
  1289. <spacer width=16 height=1>The driver program and program outputs are shown in 
  1290. <a href="^Code::c:s0p0"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 10.1</a>, parts 12 and 13. Each of the four code 
  1291. segments in <b>main</b> is similar, so we discuss only the first 
  1292. segment, which deals with a <b>Boss</b> object. <br>
  1293. <spacer width=16 height=1>Line 302<br>
  1294.  
  1295. </page>
  1296. <page>
  1297. <font size=2><br></font><font size=11><pre>
  1298. Boss b( "John", "Smith", 800.00 );<p>
  1299. </pre></font>
  1300. instantiates derived-class object b of class <b>Boss</b> and 
  1301. provides the constructor arguments including the first 
  1302. name, the last name and the fixed weekly salary.<br>
  1303. <spacer width=16 height=1>Line 303<br>
  1304. <font size=2><br></font><font size=11><pre>
  1305. b.print();               // static binding<p>
  1306. </pre></font>
  1307. explicitly invokes the <b>Boss</b> version of member function 
  1308. <b>print</b> by using the dot member selection operator off 
  1309. the specific <b>Boss</b> object <b>b</b>. This is an example of static 
  1310. binding because the type of the object for which the 
  1311. function is being called is known at compile time. This 
  1312. call is included for comparison purposes to illustrate <br>
  1313.  
  1314. </page>
  1315. <page>
  1316. that the correct <b>print</b> function is invoked using dynamic 
  1317. binding.<br>
  1318. <spacer width=16 height=1>Line 304<br>
  1319. <font size=2><br></font><font size=11><pre>
  1320. cout << " earned $" << b.earnings();<p>
  1321.     // static binding<p>
  1322. </pre></font>
  1323. explicitly invokes the <b>Boss</b> version of member function 
  1324. <b>earnings</b> by using the dot member selection operator 
  1325. off the specific <b>Boss</b> object <b>b</b>. This is also an example 
  1326. of static binding. This call is also included for 
  1327. comparison purposes, this time to illustrate that the 
  1328. correct <b>earnings</b> function is invoked using dynamic 
  1329. binding.<br>
  1330.  
  1331. </page>
  1332. <page>
  1333. Line 305<br>
  1334. <font size=2><br></font><font size=11><pre>
  1335. virtualViaPointer( &b );  <p>
  1336.     // uses dynamic binding<p>
  1337. </pre></font>
  1338. invokes function <b>virtualViaPointer</b> (line 331) with the 
  1339. address of derived class object <b>b</b>. The function receives 
  1340. this address in its parameter <b>baseClassPtr</b> which is 
  1341. declared as a <b>const Employee *</b>. This is precisely how 
  1342. to effect polymorphic behavior.<br>
  1343. <spacer width=16 height=1>Line 333<br>
  1344. <font size=2><br></font><font size=11><pre>
  1345. baseClassPtr->print();<p>
  1346. </pre></font>
  1347. invokes member function <b>print</b> of the object pointed to 
  1348. by <b>baseClassPtr</b>. Because <b>print</b> is declared <b>virtual</b> in 
  1349. the base class, the system invokes the derived class <br>
  1350.  
  1351. </page>
  1352. <page>
  1353. object's <b>print</b> function--precisely what is called 
  1354. polymorphic behavior. This function call is an example 
  1355. of dynamic binding--the <b>virtual</b> function is invoked 
  1356. through a base-class pointer, so the decision as to what 
  1357. function to invoke is deferred until execution time.<br>
  1358. <spacer width=16 height=1>Line 334<br>
  1359. <font size=2><br></font><font size=11><pre>
  1360. cout << " earned $" << baseClassPtr->earnings();<p>
  1361. </pre></font>
  1362. invokes the <b>earnings</b> member function of the object 
  1363. pointed to by <b>baseClassPtr</b>. Because <b>earnings</b> is 
  1364. declared as a <b>virtual</b> function in the base class, the 
  1365. system invokes the derived-class object's <b>earnings</b> 
  1366. function. This, too, is dynamic binding. <br>
  1367.  
  1368. </page>
  1369. <page>
  1370. Line 306<br>
  1371. <font size=2><br></font><font size=11><pre>
  1372. virtualViaReference( b );<p>
  1373.     // uses dynamic binding<p>
  1374. </pre></font>
  1375. invokes function <b>virtualViaReference</b> (line 339) to 
  1376. demonstrate that polymorphism can also be 
  1377. accomplished with <b>virtual</b> functions called off base-
  1378. class references. The function receives object <b>b</b> in 
  1379. parameter <b>baseClassRef</b> which is declared as a <b>const 
  1380. Employee &</b>. This is precisely how to effect 
  1381. polymorphic behavior with references.<br>
  1382. <spacer width=16 height=1>Line 341<br>
  1383. <font size=2><br></font><font size=11><pre>
  1384. baseClassRef.print();<p>
  1385. </pre></font>
  1386.  
  1387. </page>
  1388. <page>
  1389. invokes the <b>print</b> member function of the object 
  1390. referred to by <b>baseClassRef</b>. Because <b>print</b> is declared 
  1391. as a <b>virtual</b> function in the base class, the system 
  1392. invokes the derived class object's <b>print</b> function. This 
  1393. function call is also an example of dynamic binding--
  1394. the function is invoked through a base-class reference, 
  1395. so the decision as to what function to invoke is deferred 
  1396. until execution time.<br>
  1397. <spacer width=16 height=1>Line 342<br>
  1398. <font size=2><br></font><font size=11><pre>
  1399. cout << " earned $" << baseClassRef.earnings();<p>
  1400. </pre></font>
  1401. invokes the <b>earnings</b> member function of the object 
  1402. referred to by <b>baseClassRef</b>. Because <b>earnings</b> is 
  1403. declared as a <b>virtual</b> function in the base class, the <br>
  1404.  
  1405. </page>
  1406. <page>
  1407. system invokes the derived-class object's <b>earnings</b> 
  1408. function. This, too, is dynamic binding. <br>
  1409.  
  1410. </page>
  1411. </section>
  1412. <section type=Body name=Default title="10.7 New Classes and Dynamic Binding">
  1413. <page>
  1414. <font size=18 bold>10.7 New Classes and Dynamic Binding</font><hr>
  1415. Polymorphism and <b>virtual</b> functions work nicely if all 
  1416. possible classes were known in advance. But they also 
  1417. work when new kinds of classes are added to systems. 
  1418. New classes are accommodated by dynamic binding 
  1419. (also called <i>late binding</i>). An object's type need not be 
  1420. known at compile time for a <b>virtual</b> function call to be 
  1421. compiled. At execution time, the <b>virtual</b> function call is 
  1422. matched with the appropriate member function of the 
  1423. called object. <br>
  1424. <spacer width=16 height=1>A screen manager program can now display new kinds 
  1425. of objects as they are added to the system without the <br>
  1426.  
  1427. </page>
  1428. <page>
  1429. screen manager needing to be recompiled. The <b>draw</b> 
  1430. function call remains the same. The new objects 
  1431. themselves contain the actual drawing capabilities. This 
  1432. makes it easy to add new capabilities to systems with 
  1433. minimal impact. It also promotes software reuse.<br>
  1434. <spacer width=16 height=1>Dynamic binding enables independent software 
  1435. vendors (ISVs) to distribute software without revealing 
  1436. proprietary secrets. Software distributions can consist 
  1437. of only header files and object files. No source code 
  1438. needs to be revealed. Software developers can then use 
  1439. inheritance to derive new classes from those provided 
  1440. by the ISVs. Software that works with the classes the 
  1441. ISVs provide will continue to work with the derived <br>
  1442.  
  1443. </page>
  1444. <page>
  1445. classes and will use (via dynamic binding) the 
  1446. overridden <b>virtual</b> functions provided in these classes.<br>
  1447. <spacer width=16 height=1>In <a href="#s9p0">Section 10.9</a>, we present an additional 
  1448. comprehensive polymorphism case study. In<a href="#s10p0"> Section 
  1449. 10.10</a>, we describe in depth precisely how 
  1450. polymorphism, virtual functions and dynamic binding 
  1451. are implemented in C++.<br>
  1452.  
  1453. </page>
  1454. <page>
  1455. <b>Select the true statement(s). </b><br>
  1456. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. Usually vendors provide only header files and object files.">
  1457. Independent software vendors usually provide source code for their proprietary libraries.   <br>
  1458. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1459. Polymorphism allows objects of new classes to be added to a system dynamically. <br>
  1460. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1461.  
  1462. </page>
  1463. </section>
  1464. <section type=Body name=Default title="10.8 Virtual Destructors">
  1465. <page>
  1466. <font size=18 bold>10.8 Virtual Destructors</font><hr>
  1467. A problem can occur when using polymorphism to 
  1468. process dynamically allocated objects of a class 
  1469. hierarchy. If an object is destroyed explicitly by 
  1470. applying the <b>delete</b> operator to a base-class pointer to 
  1471. the object, the base-class destructor function is called 
  1472. on the object. This occurs regardless of the type of the 
  1473. object to which the base-class pointer is pointing and 
  1474. regardless of the fact that each class's destructor has a 
  1475. different name. <br>
  1476. <spacer width=16 height=1><a href="^Errors::c:s0p1"><img src="bckgrnds/icons/cpe_ico.gif" align=sidebar></a>There is a simple solution to this problem--declare a 
  1477. <b>virtual</b> base-class destructor. This automatically makes <br>
  1478.  
  1479. </page>
  1480. <page>
  1481. all derived-class destructors <b>virtual</b> even though they 
  1482. do not have the same name as the base-class destructor. 
  1483. Now, if an object in the hierarchy is destroyed explicitly 
  1484. by applying the <b>delete</b> operator to a base-class pointer 
  1485. to a derived-class object, the destructor for the 
  1486. appropriate class is called.  <a href="^Practice::c:s0p2"><img src="bckgrnds/icons/gpp_ico.gif" align=sidebar></a>Remember, when a derived-
  1487. class object is destroyed, the base-class part of the 
  1488. derived-class object is also destroyed--the base-class 
  1489. destructor automatically executes after the derived-
  1490. class destructor.<br>
  1491.  
  1492. </page>
  1493. <page>
  1494. <b>Select the true statement(s). </b><br>
  1495. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1496. Constructors cannot be declared virtual.  <br>
  1497. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1498. Any destructor in a class hierarchy can be declared virtual. <br>
  1499. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1500. Declaring a base class destructor virtual results in all derived class destructors being implicitly virtual. <br>
  1501. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1502.  
  1503. </page>
  1504. </section>
  1505. <section type=Body name=Default title="10.9 Case Study: Inheriting Interface and Implementation">
  1506. <page>
  1507. <font size=18 bold>10.9 Case Study: Inheriting Interface and 
  1508. Implementation</font><hr>
  1509. Our next example (<a href="^Code::c:s0p1"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 10.2</a>) re-examines the <b>Point</b>, 
  1510. <b>Circle</b>, <b>Cylinder</b> hierarchy from the previous chapter 
  1511. except that we now head the hierarchy with abstract 
  1512. base class <b>Shape</b>. <b>Shape</b> has two pure <b>virtual</b> 
  1513. functions--<b>printShapeName</b> and <b>print</b>--so <b>Shape</b> is 
  1514. an abstract base class. <b>Shape</b> contains two other <b>virtual</b> 
  1515. functions, <b>area</b> and <b>volume</b>, each of which has a default 
  1516. implementation that returns a value of zero. <b>Point</b> 
  1517. inherits these implementations from <b>Shape</b>. This makes 
  1518. sense because both the area and volume of a point are <br>
  1519.  
  1520. </page>
  1521. <page>
  1522. zero. <b>Circle</b> inherits the <b>volume</b> function from <b>Point</b>, 
  1523. but <b>Circle</b> provides its own implementation for the 
  1524. <b>area</b> function. <b>Cylinder</b> provides its own 
  1525. implementations for both the <b>area</b> function and the 
  1526. <b>volume</b> function.<br>
  1527. <spacer width=16 height=1>Note that although <b>Shape</b> is an abstract base class, it 
  1528. still contains implementations of certain member 
  1529. functions, and these implementations are inheritable. 
  1530. The <b>Shape</b> class provides an inheritable interface in the 
  1531. form of four <b>virtual</b> functions that all members of the 
  1532. hierarchy will contain. The <b>Shape</b> class also provides 
  1533. some implementations that derived classes in the first 
  1534. few levels of the <a href="^Engineer::c:s0p12"><img src="bckgrnds/icons/seo_ico.gif" align=sidebar></a>hierarchy will use.<br>
  1535.  
  1536. </page>
  1537. <page>
  1538. Base class <b>Shape</b> (<a href="^Code::c:s0p1"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 10.2</a>, part 1) consists of four 
  1539. <b>public virtual</b> functions and does not contain any data. 
  1540. Functions <b>printShapeName</b> and <b>print</b> are pure <b>virtual</b>, 
  1541. so they are overridden in each of the derived classes. 
  1542. Functions <b>area</b> and <b>volume</b> are defined to return <b>0.0</b>. 
  1543. These functions are overridden in derived classes when 
  1544. it is appropriate for those classes to have a different 
  1545. <b>area</b> calculation and/or a different <b>volume</b> calculation. 
  1546. Note that <b>Shape</b> is an abstract class and it contains 
  1547. some "impure" <b>virtual</b> functions (<b>area</b> and <b>volume</b>). 
  1548. Abstract classes can also include non-<b>virtual</b> functions 
  1549. and data which will be inherited by derived classes.<br>
  1550.  
  1551. </page>
  1552. <page>
  1553. Class <b>Point</b> (<a href="^Code::c:s0p1"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 10.2</a>, parts 2 and 3) is derived from 
  1554. <b>Shape</b> with <b>public</b> inheritance. A <b>Point</b> has an area of 
  1555. <b>0.0</b> and a volume of <b>0.0</b>, so the base-class member 
  1556. functions <b>area</b> and <b>volume</b> are not overridden here--
  1557. they are simply inherited as defined in <b>Shape</b>. 
  1558. Functions <b>printShapeName</b> and <b>print</b> are 
  1559. implementations of <b>virtual</b> functions that were defined 
  1560. as pure <b>virtual</b> in the base class--if we did not override 
  1561. these functions in class <b>Point</b>, then <b>Point</b> would also be 
  1562. an abstract class and we would not be able to instantiate 
  1563. <b>Point</b> objects. Other member functions include a <i>set</i> 
  1564. function to assign new <b>x</b> and <b>y</b> coordinates to a <b>Point</b> <br>
  1565.  
  1566. </page>
  1567. <page>
  1568. and <i>get</i> functions to return the <b>x</b> and <b>y</b> coordinates of a 
  1569. <b>Point</b>. <br>
  1570. <spacer width=16 height=1>Class <b>Circle</b> (<a href="^Code::c:s0p1"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 10.2</a>, parts 4 and 5) is derived from 
  1571. <b>Point</b> with <b>public</b> inheritance. A <b>Circle</b> has a volume 
  1572. of <b>0.0</b>, so base-class member function <b>volume</b> is not 
  1573. overridden here--it is inherited from <b>Point</b> which 
  1574. previously inherited <b>volume</b> from <b>Shape</b>. A <b>Circle</b> has 
  1575. non-zero area, so the <b>area</b> function is overridden in this 
  1576. class. Functions <b>printShapeName</b> and <b>print</b> are 
  1577. implementations of <b>virtual</b> functions that were defined 
  1578. as pure <b>virtual</b> in the <b>Shape</b> class. If these functions are 
  1579. not overridden here, the <b>Point</b> versions of these 
  1580. functions would be inherited. Other member functions <br>
  1581.  
  1582. </page>
  1583. <page>
  1584. include a <i>set</i> function to assign a new <b>radius</b> to a <b>Circle</b> 
  1585. and a <i>get</i> function to return the <b>radius</b> of a <b>Circle</b>. <br>
  1586. <spacer width=16 height=1>Class <b>Cylinder</b> (<a href="^Code::c:s0p1"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 10.2</a>, parts 6 and 7) is derived 
  1587. from <b>Circle</b> with <b>public</b> inheritance. A <b>Cylinder</b> has 
  1588. area and volume different from those of <b>Circle</b>, so the 
  1589. <b>area</b> and <b>volume</b> functions are both overridden in this 
  1590. class. Functions <b>printShapeName</b> and <b>print</b> are 
  1591. implementations of <b>virtual</b> functions that were defined 
  1592. as pure <b>virtual</b> in the <b>Shape</b> class. If these functions are 
  1593. not overridden here, the <b>Circle</b> versions of these 
  1594. functions would be inherited. Other member functions 
  1595. include <i>set</i> and <i>get</i> functions to assign a new <b>height</b> and 
  1596. return the <b>height</b> of a <b>Cylinder</b>, respectively.<br>
  1597.  
  1598. </page>
  1599. <page>
  1600. The driver program (<a href="^Code::c:s0p1"><img src="bckgrnds/icons/code_ico.gif" align=sidebar>Fig. 10.2</a>, parts 8, 9 and 10) begins 
  1601. by instantiating <b>Point</b> object <b>point</b>, <b>Circle</b> object <b>circle</b> 
  1602. and <b>Cylinder</b> object <b>cylinder</b>. Functions 
  1603. <b>printShapeName</b> and <b>print</b> are invoked for each 
  1604. object to print the name of the object and to illustrate 
  1605. that the objects are initialized correctly. Each call to 
  1606. <b>printShapeName</b> and <b>print</b> in lines 164 through 173 
  1607. uses static binding--at compile time the compiler 
  1608. knows the type of each object for which 
  1609. <b>printShapeName</b> and <b>print</b> are called. <br>
  1610. <spacer width=16 height=1>Next, array <b>arrayOfShapes</b>, each element of which is 
  1611. of type <b>Shape *</b>, is declared. This array of base-class 
  1612. pointers is used to point to each of the derived-class <br>
  1613.  
  1614. </page>
  1615. <page>
  1616. objects. The address of object <b>point</b> is assigned to 
  1617. <b>arrayOfShapes[ 0 ]</b> (line 179), the address of object 
  1618. <b>circle</b> is assigned to <b>arrayOfShapes[ 1 ]</b> (line 182) and 
  1619. the address of object <b>cylinder</b> is assigned to 
  1620. <b>arrayOfShapes[ 2 ]</b> (line 185). <br>
  1621. <spacer width=16 height=1>Next, a <b>for</b> structure (line 193) walks through the array 
  1622. <b>arrayOfShapes</b> and invokes function 
  1623. <b>virtualViaPointer</b> (line 194)<br>
  1624. <font size=2><br></font><font size=11><pre>
  1625. virtualViaPointer( arrayOfShapes[ i ] );<p>
  1626. </pre></font>
  1627. for each element of the array. Function 
  1628. <b>virtualViaPointer</b> receives in parameter <b>baseClassPtr</b> 
  1629. (of type <b>const Shape *</b>) the address stored in an 
  1630. element of <b>arrayOfShapes</b>. Each time <br>
  1631.  
  1632. </page>
  1633. <page>
  1634. <b>virtualViaPointer</b> executes, the following four <b>virtual</b> 
  1635. function calls are made <br>
  1636. <font size=2><br></font><font size=11><pre>
  1637. baseClassPtr->printShapeName()<p>
  1638. baseClassPtr->print()<p>
  1639. baseClassPtr->area()<p>
  1640. baseClassPtr->volume()<p>
  1641. </pre></font>
  1642. Each of these calls invokes a <b>virtual</b> function on the 
  1643. object to which <b>baseClassPtr</b> points at execution 
  1644. time--an object whose type cannot be determined here 
  1645. at compile time. The output illustrates that the 
  1646. appropriate functions for each class are invoked. First, 
  1647. the string "<b>Point: </b>" and the coordinates of the object 
  1648. <b>point</b> are output; the area and volume are both <b>0.00</b>. <br>
  1649.  
  1650. </page>
  1651. <page>
  1652. Next, the string "<b>Circle: </b>", the coordinates of the 
  1653. center of object <b>circle</b> and the radius of object <b>circle</b> are 
  1654. output; the area of <b>circle</b> is calculated and the volume is 
  1655. returned as <b>0.00</b>. Finally, the string "<b>Cylinder: </b>", the 
  1656. coordinates of the center of the base of object <b>cylinder</b>, 
  1657. the radius of object <b>cylinder</b> and the height of object 
  1658. <b>cylinder</b> are output; the area of <b>cylinder</b> is calculated 
  1659. and the volume of <b>cylinder</b> is calculated. All the 
  1660. <b>virtual</b> function calls to <b>printShapeName</b>, <b>print</b>, <b>area</b> 
  1661. and <b>volume</b> are resolved at run-time with dynamic 
  1662. binding.<br>
  1663.  
  1664. </page>
  1665. <page>
  1666. Finally, a <b>for</b> structure (line 202) walks through 
  1667. <b>arrayOfShapes</b> and invokes function 
  1668. <b>virtualViaReference</b> (line 203)<br>
  1669. <font size=2><br></font><font size=11><pre>
  1670. virtualViaReference( *arrayOfShapes[ j ] );<p>
  1671. </pre></font>
  1672. for each element of the array. Function 
  1673. <b>virtualViaReference</b> receives in its parameter 
  1674. <b>baseClassRef</b> (of type <b>const Shape &</b>), a reference 
  1675. formed by dereferencing the address stored in an 
  1676. element of the array. During each call to 
  1677. <b>virtualViaReference</b>, the following <b>virtual</b> function 
  1678. calls are made <br>
  1679. <font size=2><br></font><font size=11><pre>
  1680. baseClassRef.printShapeName()<p>
  1681. baseClassRef.print()<p><p>
  1682. </pre></font>
  1683.  
  1684. </page>
  1685. <page>
  1686. <font size=2><br></font><font size=11><pre>
  1687. baseClassRef.area()<p>
  1688. baseClassRef.volume()<p>
  1689. </pre></font>
  1690. Each of the preceding calls invokes these functions on 
  1691. the object to which <b>baseClassRef</b> refers. The output 
  1692. produced using base-class references is identical to the 
  1693. output produced using base-class pointers.<br>
  1694.  
  1695. </page>
  1696. </section>
  1697. <section type=Body name=Default title="10.10 Polymorphism, virtual Functions and Dynamic Binding "Under the Hood"">
  1698. <page>
  1699. <font size=18 bold>10.10 Polymorphism, <tt>virtual</tt> Functions and 
  1700. Dynamic Binding "Under the Hood"</font><hr>
  1701. C++ makes polymorphism easy to program. It is 
  1702. certainly possible to program for polymorphism in non-
  1703. object-oriented languages such as C, but doing so 
  1704. requires complex and potentially dangerous pointer 
  1705. manipulations. In this section we discuss how C++ 
  1706. implements polymorphism, <b>virtual</b> functions and 
  1707. dynamic binding internally. This will give you a solid 
  1708. understanding of how these capabilities really work. 
  1709. More importantly, it will help you appreciate the 
  1710. overhead of polymorphism--in additional memory <br>
  1711.  
  1712. </page>
  1713. <page>
  1714. consumption and processor time. This will help you 
  1715. determine when to use polymorphism and when to 
  1716. avoid it. As you will see in Chapter 20, "Standard 
  1717. Template Library (STL)," the STL components were 
  1718. implemented without polymorphism and <b>virtual</b> 
  1719. functions--this was done to avoid execution-time 
  1720. overhead and achieve optimal performance to meet the 
  1721. unique requirements of the STL.<br>
  1722. <spacer width=16 height=1>First, we will explain the data structures the C++ 
  1723. compiler builds at compile time to support 
  1724. polymorphism at execution time. Then, we will show 
  1725. how an executing program uses these data structures to <br>
  1726.  
  1727. </page>
  1728. <page>
  1729. execute <b>virtual</b> functions and achieve the dynamic 
  1730. binding associated with polymorphism.<br>
  1731. <spacer width=16 height=1>When C++ compiles a class that has one or more 
  1732. <b>virtual</b> functions, it builds a <b><i>virtual function table 
  1733. (vtable)</i></b> for that class. The <i>vtable</i> is used by the 
  1734. executing program to select the proper function 
  1735. implementations each time a <b>virtual</b> function of that 
  1736. class is to be executed. <a href="^Illustration::c:s0p1"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Figure 10.3</a> illustrates the 
  1737. <b>virtual</b> function tables for classes <b>Shape</b>, <b>Point</b>, <b>Circle</b> 
  1738. and <b>Cylinder</b>.<br>
  1739. <spacer width=16 height=1>In the <i>vtable</i> for class <b>Shape</b>, the first function pointer 
  1740. points to the implementation of the <b>area</b> function for 
  1741. that class, namely a function that returns an area of <b>0.0</b>. <br>
  1742.  
  1743. </page>
  1744. <page>
  1745. The second function pointer points to the <b>volume</b> 
  1746. function which also returns <b>0.0</b>. The <b>printShapeName</b> 
  1747. and <b>print</b> functions are each pure <b>virtual</b>--they lack 
  1748. implementations so their function pointers are each set 
  1749. to <b>0</b>. Any class that has one or more <b>0</b> pointers in its 
  1750. <i>vtable</i> is an abstract class. Classes without any <b>0</b> <i>vtable</i> 
  1751. pointers (as <b>Point</b>, <b>Circle</b> and <b>Cylinder</b>) are concrete 
  1752. classes.<br>
  1753. <spacer width=16 height=1>Class <b>Point</b> inherits the <b>area</b> and <b>volume</b> functions of 
  1754. class <b>Shape</b>, so the compiler simply sets these two 
  1755. pointers in the <i>vtable</i> for class <b>Point</b> to be copies of the 
  1756. <b>area</b> and <b>volume</b> pointers in class <b>Shape</b>. Class <b>Point</b> 
  1757. overrides function <b>printShapeName</b> to print <tt><b>"Point: "</b></tt> <br>
  1758.  
  1759. </page>
  1760. <page>
  1761. so the function pointer points to the <b>printShapeName</b> 
  1762. function of class <b>Point</b>. <b>Point</b> also overrides <b>print</b> so 
  1763. the corresponding function pointer points to the <b>Point</b> 
  1764. class function that prints <b>[ x, y ]</b>.<br>
  1765. <spacer width=16 height=1>The <b>Circle area</b> function pointer in the <i>vtable</i> for class 
  1766. <b>Circle</b> points to the <b>Circle area</b> function that returns 
  1767. p<b>r2</b>. The <b>volume</b> function pointer is simply copied 
  1768. from the <b>Point</b> class--that pointer was previously 
  1769. copied into <b>Point</b> from <b>Shape</b>. The <b>printShapeName</b> 
  1770. function pointer points to the <b>Circle</b> version of the 
  1771. function which prints <tt><b>"Circle: "</b></tt>. The <b>print</b> function 
  1772. pointer points to <b>Circle</b>'s <b>print</b> function that prints <b>[ x, 
  1773. y ] r</b>. <br>
  1774.  
  1775. </page>
  1776. <page>
  1777. The <b>Cylinder</b> <b>area</b> function pointer in the <i>vtable</i> for 
  1778. class <b>Cylinder</b> points to the <b>Cylinder area</b> function 
  1779. that calculates the surface area of the <b>Cylinder</b>, namely 
  1780. <b>2</b>p<b>r2 + 2</b>p<b>rh</b>. The <b>Cylinder</b> <b>volume</b> function pointer 
  1781. points to a <b>volume</b> function that returns p<b>r2h</b>. The 
  1782. <b>Cylinder printShapeName</b> function pointer points to a 
  1783. function that prints <tt><b>"Cylinder: "</b></tt>. The <b>Cylinder print</b> 
  1784. function pointer points to its function that prints <b>[ x, y ] 
  1785. r h</b>.<br>
  1786. <spacer width=16 height=1>Polymorphism is accomplished through a complex data 
  1787. structure that involves three levels of pointers. So far, 
  1788. we have discussed only one level--the function 
  1789. pointers in the <i>vtable</i>. These pointers point to the actual <br>
  1790.  
  1791. </page>
  1792. <page>
  1793. functions to be executed when a <b>virtual</b> function is 
  1794. invoked.<br>
  1795. <spacer width=16 height=1>Now we consider the second level of pointers. 
  1796. Whenever an object of a class with <b>virtual</b> functions is 
  1797. instantiated, the compiler attaches to the front of the 
  1798. object a pointer to the <i>vtable</i> for that class. [Note: This 
  1799. pointer is normally at the front of the object, but it is not 
  1800. required to be implemented that way.]<br>
  1801. <spacer width=16 height=1>The third level of pointer is simply the handle on the 
  1802. object that is receiving the <b>virtual</b> function call (this 
  1803. handle may also be a reference).<br>
  1804. <spacer width=16 height=1>Now let us see how a typical <b>virtual</b> function call is 
  1805. executed. Consider the call <br>
  1806.  
  1807. </page>
  1808. <page>
  1809. <font size=2><br></font><font size=11><pre>
  1810. baseClassPtr->printShapeName()<p>
  1811. </pre></font>
  1812. in function <b>virtualViaPointer</b>. Assume for the 
  1813. following discussion that <b>baseClassPtr</b> contains the 
  1814. address in <b>arrayOfShapes[ 1 ]</b> (i.e., the address of 
  1815. object <b>circle</b>). When the compiler compiles this 
  1816. statement, it determines that the call is indeed being 
  1817. made off a base-class pointer and that 
  1818. <b>printShapeName</b> is a <b>virtual</b> function.<br>
  1819. <spacer width=16 height=1>Next, the compiler determines that <b>printShapeName</b> is 
  1820. the third entry in each of the <i>vtables</i>. To locate this 
  1821. entry, the compiler notes that it will need to skip the 
  1822. first two entries. Thus, the compiler compiles an <i>offset</i> 
  1823. or <i>displacement</i> of 8 bytes (4 bytes for each pointer on <br>
  1824.  
  1825. </page>
  1826. <page>
  1827. today's popular 32-bit machines) into the machine 
  1828. language object code that will execute the <b>virtual</b> 
  1829. function call.<br>
  1830. <spacer width=16 height=1>Then, the compiler generates code that will (Note: The 
  1831. numbers in the list below correspond to the circled 
  1832. numbers in<a href="^Illustration::c:s0p1"> <img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 10.3</a>):<br>
  1833. 1. Select the <i>ith</i> entry from <b>arrayOfShapes</b> (in this case 
  1834. the address of object <b>circle</b>) and pass it to <b>virtualViaPointer</b>. This sets <b>baseClassPtr</b> to point to <b>circle</b>.<br>
  1835. 2. Dereference that pointer to get to the <b>circle</b> object--
  1836. which as you recall, begins with a pointer to the <b>Circle</b> 
  1837. <i>vtable</i>.<br>
  1838.  
  1839. </page>
  1840. <page>
  1841. 3. Dereference <b>circle</b>'s <i>vtable</i> pointer to get to the 
  1842. <b>Circle</b> <i>vtable</i>.<br>
  1843. 4. Skip the offset of 8 bytes to pick up the <b>printShapeName</b> function pointer.<br>
  1844. 5. Dereference the <b>printShapeName</b> function pointer 
  1845. to form the name of the actual function to be executed 
  1846. and use the function call operator <tt><b>()</b></tt> to execute the 
  1847. appropriate <b>printShapeName</b> function and print the 
  1848. character string <tt><b>"Circle: "</b></tt>.<br>
  1849. The data structures of <a href="^Illustration::c:s0p1"><img src="bckgrnds/icons/ill_ico.gif" align=sidebar>Fig. 10.3</a> may appear to be 
  1850. complex, but most of this complexity is managed by the 
  1851. compiler and hidden from the programmer, making 
  1852. polymorphic programming straightforward in C++.<br>
  1853.  
  1854. </page>
  1855. <page>
  1856. <a href="^Perform::c:s0p0"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>The pointer dereferencing operations and memory 
  1857. accesses that occur on every <b>virtual</b> function call do 
  1858. require some additional execution time. The <i>vtables</i> and 
  1859. the <i>vtable</i> pointers added to the objects require some 
  1860. additional memory.<br>
  1861. <spacer width=16 height=1><a href="^Perform::c:s0p1"><img src="bckgrnds/icons/perf_ico.gif" align=sidebar></a>Hopefully, you now have enough information about 
  1862. how <b>virtual</b> functions operate to determine if using 
  1863. them is appropriate for each application you are 
  1864. considering.<br>
  1865.  
  1866. </page>
  1867. <page>
  1868. <b>Select the true statement(s). </b><br>
  1869. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1870. When a class containing virtual functions is compiled, a virtual function table is created for that class.  <br>
  1871. <component type="checkbox" width=20 height=18 label="" name=""  correct="True.">
  1872. A virtual function table is also known as a vtable.  <br>
  1873. <component type="checkbox" width=20 height=18 label="" name=""  feedback="False. The class is an abstract class if it has one or more 0s in its vtable. The 0s represent the class's pure virtual functions.">
  1874. Any class that has one or more 0s in its vtable is a concrete class.   <br>
  1875. <component type=button name=b label="Check Your Answer" width=125 height=24>
  1876.  
  1877. </page>
  1878. </section>
  1879. <section type=Body name=Default title="10.11 Summary">
  1880. <page>
  1881. <font size=18 bold>10.11 Summary</font><hr>
  1882. <indent width=8 delay>*   With <b>virtual</b> functions and polymorphism, it 
  1883. becomes possible to design and implement systems that 
  1884. are more easily extensible. Programs can be written to 
  1885. process objects of types that may not exist when the 
  1886. program is under development.</indent>
  1887. <indent width=8 delay>*  Polymorphic programming with <b>virtual</b> functions 
  1888. can eliminate the need for <b>switch</b> logic. The programmer can use the <b>virtual</b> function mechanism to perform 
  1889. the equivalent logic automatically, thus avoiding the 
  1890. kinds of errors typically associated with <b>switch</b> logic. 
  1891. Client code making decisions about object types and </indent>
  1892.  
  1893. </page>
  1894. <page>
  1895. <indent width=8 delay>*   representations indicates poor class design.</indent>
  1896. <indent width=8 delay>*   Derived classes can provide their own implementations of a base class <b>virtual</b> function if necessary, but if 
  1897. they do not, the base class's implementation is used.</indent>
  1898. <indent width=8 delay>*   If a <b>virtual</b> function is called by referencing a specific object by name and using the dot member selection operator, the reference is resolved at compile time 
  1899. (this is called <i>static binding</i>) and the <b>virtual</b> function 
  1900. that is called is the one defined for (or inherited by) the 
  1901. class of that particular object.</indent>
  1902. <indent width=8 delay>*  There are many situations in which it is useful to 
  1903. define classes for which the programmer never intends 
  1904. to instantiate any objects. Such classes are called </indent>
  1905.  
  1906. </page>
  1907. <page>
  1908. <indent width=8 delay>*   abstract classes. Because these are used only as base 
  1909. classes, we will normally refer to them as abstract base 
  1910. classes. No objects of an abstract class may be 
  1911. instantiated in a program.</indent>
  1912. <indent width=8 delay>*   Classes from which objects can be instantiated are 
  1913. called concrete classes.</indent>
  1914. <indent width=8 delay>*   A class is made abstract by declaring one or more of 
  1915. its <b>virtual</b> functions to be pure. A pure <b>virtual</b> function 
  1916. is one with an initializer of<b> = 0</b> in its declaration.</indent>
  1917. <indent width=8 delay>*  If a class is derived from a class with a pure <b>virtual</b> 
  1918. function without supplying a definition for that pure 
  1919. <b>virtual</b> function in the derived class, then that <b>virtual</b> 
  1920. function remains pure in the derived class. Conse</indent>
  1921.  
  1922. </page>
  1923. <page>
  1924. <indent width=8 delay>*   quently, the derived class is also an abstract class (and 
  1925. cannot have any objects).</indent>
  1926. <indent width=8 delay>*   C++ enables polymorphism--the ability for objects 
  1927. of different classes related by inheritance to respond 
  1928. differently to the same member function call. </indent>
  1929. <indent width=8 delay>*   Polymorphism is implemented via <b>virtual</b> functions. </indent>
  1930. <indent width=8 delay>*   When a request is made through a base-class pointer 
  1931. or reference to use a <b>virtual</b> function, C++ chooses the 
  1932. correct overridden function in the appropriate derived 
  1933. class associated with the object.</indent>
  1934. <indent width=8 delay>*  Through the use of <b>virtual</b> functions and polymorphism, one member function call can cause different 
  1935. actions depending on the type of the object receiving </indent>
  1936.  
  1937. </page>
  1938. <page>
  1939. <indent width=8 delay>*   the call. </indent>
  1940. <indent width=8 delay>*   Although we cannot instantiate objects of abstract 
  1941. base classes, we can declare pointers to abstract base 
  1942. classes. Such pointers can be used to enable polymorphic manipulations of derived-class objects when such 
  1943. objects are instantiated from concrete classes.</indent>
  1944. <indent width=8 delay>*   New kinds of classes are regularly added to systems. 
  1945. New classes are accommodated by dynamic binding 
  1946. (also called late binding). The type of an object need 
  1947. not be known at compile time for a <b>virtual</b> function call 
  1948. to be compiled. At execution time, the <b>virtual</b> function 
  1949. call is matched with the member function of the receiving object. </indent>
  1950.  
  1951. </page>
  1952. <page>
  1953. <indent width=8 delay>*   Dynamic binding enables independent software vendors (ISVs) to distribute software without revealing 
  1954. proprietary secrets. Software distributions can consist 
  1955. of only header files and object files. No source code 
  1956. needs to be revealed. Software developers can then use 
  1957. inheritance to derive new classes from those provided 
  1958. by the ISVs. The software that works with the classes 
  1959. the ISVs provide will continue to work with the derived 
  1960. classes and will use (via dynamic binding) the overridden <b>virtual</b> functions provided in these classes.</indent>
  1961. <indent width=8 delay>*  Dynamic binding requires that at execution time, the 
  1962. call to a <b>virtual</b> member function be routed to the <b>virtual</b> function version appropriate for the class. A <b>vir</b></indent>
  1963.  
  1964. </page>
  1965. <page>
  1966. <indent width=8 delay>*   <b>tual</b> function table called the <i>vtable</i> is implemented as 
  1967. an array containing function pointers. Each class that 
  1968. contains <b>virtual</b> functions has a <i>vtable</i>. For each <b>virtual</b> function in the class, the <i>vtable</i> has an entry containing a function pointer to the version of the <b>virtual</b> 
  1969. function to use for an object of that class. The <b>virtual</b> 
  1970. function to use for a particular class could be the function defined in that class, or it could be a function inherited either directly or indirectly from a base class higher 
  1971. in the hierarchy.</indent>
  1972. <indent width=8 delay>*  When a base class provides a <b>virtual</b> member function, derived classes can override the <b>virtual</b> function, 
  1973. but they do not have to override it. Thus a derived class </indent>
  1974.  
  1975. </page>
  1976. <page>
  1977. <indent width=8 delay>*   can use a base class's version of a <b>virtual</b> member 
  1978. function, and this would be indicated in the <i>vtable</i>.</indent>
  1979. <indent width=8 delay>*   Each object of a class with <b>virtual</b> functions contains 
  1980. a pointer to the <i>vtable</i> for that class. The appropriate 
  1981. function pointer in the <i>vtable</i> is obtained and dereferenced to complete the call at execution time. This 
  1982. <i>vtable</i> lookup and pointer dereferencing require nominal execution time overhead, usually less than the best 
  1983. possible client code.</indent>
  1984. <indent width=8 delay>*  Declare the base-class destructor <b>virtual</b> if the class 
  1985. contains <b>virtual</b> functions. This makes all derived-class 
  1986. destructors <b>virtual</b> even though they do not have the 
  1987. same name as the base-class destructor. If an object in </indent>
  1988.  
  1989. </page>
  1990. <page>
  1991. <indent width=8 delay>*   the hierarchy is destroyed explicitly by applying the 
  1992. <b>delete</b> operator to a base-class pointer to a derived-class 
  1993. object, the destructor for the appropriate class is called.</indent>
  1994. <indent width=8 delay>*   Any class that has one or more <b>0</b> pointers in its <i>vtable</i> 
  1995. is an abstract class. Classes without any <b>0</b> <i>vtable</i> pointers (like <b>Point</b>, <b>Circle</b> and <b>Cylinder</b>) are concrete 
  1996. classes.</indent>
  1997.  
  1998. </page>
  1999. <page>
  2000. <br>
  2001.  
  2002. </page>
  2003. </section>
  2004.  
  2005. <section type=Popup name=Debug title="Testing">
  2006. <page>
  2007. This chapter does not contain any Testing and Debugging tips.
  2008. </page>
  2009. </section>
  2010. <section type=Popup name=Portable title="Portability">
  2011. <page>
  2012. This chapter does not contain any Portability tips.
  2013. </page>
  2014. </section>
  2015. <section type=Popup name=AppletPopup title="Applet Examples">
  2016. <page>
  2017. This chapter does not contain any Applet Examples.
  2018. </page>
  2019. </section>
  2020. </chapter>
  2021. </html>
  2022. </html>
  2023.